package gconver

import (
	"fmt"
	"github.com/goinggo/mapstructure"
	"reflect"
)

// map to struct
func MapToStruct(obj interface{}, mapData map[interface{}]interface{}) error {
	if err := mapstructure.Decode(mapData, obj); err != nil {
		fmt.Println(err)
		return err
	}

	return nil
}

// struct to map
func StructToMap(obj interface{}, mapData map[string]interface{}) {
	t := reflect.TypeOf(obj)
	v := reflect.ValueOf(obj)

	for i := 0; i < t.NumField(); i++ {
		mapData[t.Field(i).Name] = v.Field(i).Interface()
	}
}
